﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace QGZX.Logic.Admin
{
    public class PerformanceController : Controller
    {

        //------------对岗位申请表的相关操作---------------------//
        #region 获取岗位申请表数据，分页展示数据--ActionResult Index()
        /// <summary>
        /// 加载岗位申请表
        /// </summary>C:\Users\Administrator\Desktop\QGZX-0715\QGZX\Areas\Admin\Views\Performance\Index.cshtml
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 根据是否在岗和审核状态获取岗位申请记录--ActionResult GetApplicationDatas()
        /// <summary>
        /// 从数据库获取岗位申请记录
        /// </summary>
        /// <returns></returns>
        public ActionResult GetApplicationDatas()
        {
            int pageSize = int.Parse(Request.Form["rows"]);

            int pageIndex = int.Parse(Request.Form["page"]);

            var lists = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetPagedList(pageIndex, pageSize, x => x.IsOnGuard == 0 && x.AuditResult == 1, y => y.PositionId).Select(x => x.ToPOCO());

            int rowCount = lists.Count();

            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = lists,
                footer = null,
            };

            return Json(dgMode);
        }
        #endregion

        #region 通过PosApplyId查找详细信息--ActionResult CheckInfo(int id)
        /// <summary>
        /// 查找选定同学的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult CheckInfo(int id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(x => x.PosApplyId == id).FirstOrDefault().ToViewModel();
            var list = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentNum == model.ApplyStudentNum).FirstOrDefault().ToViewModel();
            if (list != null) {
                int Gender = list.Gender;
                int Grade = (int)list.Grade;
                string College = OperateContext.Current.BLLSession.Iqgzx_collegeBLL.GetListBy(x => x.CollegeId == list.College).FirstOrDefault().CollegeName;
                string Profession = OperateContext.Current.BLLSession.Iqgzx_professionBLL.GetListBy(x => x.ProfessionId == list.Profession).FirstOrDefault().ProfessionName;
                if (Gender == 0)
                {
                    ViewBag.Gender = "男";
                }
                else
                {
                    ViewBag.Gender = "女";
                }
                ViewBag.Grade = Grade;
                ViewBag.College = College;
                ViewBag.Profession = Profession;
            }
            return PartialView(model);
        }
        #endregion

        //------------绩效登记的相关操作---------------------//
        #region 从岗位申请表中加载数据并分页显示待登记绩效记录--ActionResult RegisterPerformance
        /// <summary>
        /// 初始化绩效登记记录，以可编辑数据表显示待登记绩效记录 需要进一步优化
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public ActionResult RegisterPerformance()
        {
            //获取页容量
            int pageSize = 0;
            if (Request.Form["rows"] != null) { pageSize = int.Parse(Request.Form["rows"]); }
            //获取请求页码
            int pageIndex = 10;
            if (Request.Form["page"] != null) { pageIndex = int.Parse(Request.Form["page"]); }
            var lists = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.GetPagedList(pageIndex, pageSize, x => x.PerforId != 0, y => y.RegisterTime).Select(x => x.ToPOCO());
            int rowCount = lists.Count();
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = lists,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载登记绩效的window + RegisterPerfor()
        /// <summary>
        /// 加载登记绩效的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult RegisterPerfor(int Id)
        {
            MODEL.ViewModel.Position_application apply = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(x => x.PosApplyId == Id).FirstOrDefault().ToPOCO().ToViewModel(); 
            MODEL.ViewModel.Performance perfor = new MODEL.ViewModel.Performance();
            if(apply != null)
            {
                var model = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.GetListBy(x => x.ApplyId == apply.PosApplyId && x.WorkStudentNum == apply.ApplyStudentNum);
                if (model.Count() == 0)
                {
                    perfor.ApplyId = Id;
                    perfor.PositionName = apply.PositionName;
                    perfor.PositionId = apply.PositionId;
                    perfor.WorkStudentNum = apply.ApplyStudentNum;
                    perfor.WorkStudentName = apply.ApplyStudentName;
                }
                else 
                { 
                    return Redirect("/admin/Performance/RegisterPerformance"); 
                }
            }
            return PartialView(perfor);
        }
        #endregion

        #region post绩效登记的信息--RegisterPerfor
        /// <summary>
        ///post绩效登记的信息 RegisterPerfor(MODEL.qgzx_performance model)
        /// </summary>
        /// <returns></returns>
        public ActionResult RegisterPerfor(MODEL.qgzx_performance model)
        {
            model.RegisterTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.Add(model);
            if (res > 0)
            {
                return Redirect("/admin/Performance/RegisterPerformance");
            }
            return Redirect("/admin/Performance/RegisterPerformance");
        }
        #endregion

        #region 加载修改绩效的window + EditPerfor()
        /// <summary>
        /// 加载修改绩效的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditPerfor(int Id)
        {
            var perfor = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.GetListBy(x => x.PerforId == Id).FirstOrDefault().ToPOCO().ToViewModel();
            return PartialView("RegisterPerfor", perfor);
        }
        #endregion

        #region post加载修改绩效信息--EditPerfor
        /// <summary>
        ///post加载修改绩效信息 EditPerfor(MODEL.qgzx_performance model)
        /// </summary>
        /// <returns></returns>
        public ActionResult EditPerfor(MODEL.qgzx_performance model)
        {
            model.RegisterTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.Modify(model, "AbsentTimes", "WorkEvaluate", "PerforScore", "RegisterTime");
            if (res > 0)
            {
                return Redirect("/admin/Performance/RegisterPerformance");
            }
            return Redirect("/admin/Performance/RegisterPerformance");
        }
        #endregion

        #region 加载审核绩效的window + AuditPerfor()
        /// <summary>
        /// 加载登记绩效的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AuditPerfor(int Id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.GetListBy(x => x.PerforId == Id).FirstOrDefault().ToPOCO().ToViewModel();
            return PartialView(model);
        }
        #endregion

        #region post绩效审核的信息--AuditPerfor
        /// <summary>
        ///post绩效登记的信息 SavePerfor(MODEL.qgzx_performance model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AuditPerfor(MODEL.qgzx_performance model)
        {
            model.AuditTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.Modify(model, "AuditStatus", "AuditReason", "AuditTime");
            if (res > 0)
            {
                return Redirect("/admin/Performance/RegisterPerformance");
            }
            return Redirect("/admin/Performance/RegisterPerformance");
        }
        #endregion

        #region 删除绩效 + DelPerfor()
        /// <summary>
        /// 删除绩效 + DelPerfor()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelPerfor()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_performanceBLL.DelBy(x => x.PerforId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion

        //------------工资发放的相关操作---------------------//
        #region 查看登记工资记录--CheckSalary()
        /// <summary>
        /// 查看登记工资记录--ActionResult CheckSalary
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckSalary()
        {
            //获取页容量
            int pageSize = 0;
            if (Request.Form["rows"] != null) { pageSize = int.Parse(Request.Form["rows"]); }
            //获取请求页码
            int pageIndex = 10;
            if (Request.Form["page"] != null) { pageIndex = int.Parse(Request.Form["page"]); }
            var lists = OperateContext.Current.BLLSession.Iqgzx_salaryBLL.GetPagedList(pageIndex, pageSize, x => x.SalaryDisID != 0, y => y.DistriTime).Select(x => x.ToPOCO());
            int rowCount = lists.Count();
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = lists,
                footer = null
            };
            return Json(dgMode);
        } 
        #endregion

        #region 加载填写工资的window + WriteSalary()
        /// <summary>
        /// 加载填写工资的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult WriteSalary(int Id)
        {
            MODEL.ViewModel.Performance perfor = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.GetListBy(x => x.PerforId == Id).FirstOrDefault().ToPOCO().ToViewModel();
            MODEL.ViewModel.Salary salary = new MODEL.ViewModel.Salary();
            if (perfor != null)
            {
                var model = OperateContext.Current.BLLSession.Iqgzx_salaryBLL.GetListBy(x => x.PerforID == perfor.PerforId && x.WorkStudentNum == perfor.WorkStudentNum);
                if(model.Count() == 0)
                {
                    salary.PerforID = Id;
                    salary.PositionName = perfor.PositionName;
                    salary.PositionId = perfor.PositionId;
                    salary.WorkStudentNum = perfor.WorkStudentNum;
                    salary.WorkStudentName = perfor.WorkStudentName;
                }
                else 
                { 
                    return Redirect("/admin/Performance/CheckSalary");
                }
            }
            return PartialView(salary);
        }
        #endregion

        #region post绩效登记的信息--WriteSalary
        /// <summary>
        ///post加载填写工资信息 WriteSalary(MODEL.qgzx_performance model)
        /// </summary>
        /// <returns></returns>
        public ActionResult WriteSalary(MODEL.qgzx_salary model)
        {
            model.DistriTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_salaryBLL.Add(model);
            if (res > 0)
            {
                return Redirect("/admin/Performance/CheckSalary");
            }
            return Redirect("/admin/Performance/CheckSalary");
        }
        #endregion

        #region 加载修改工资记录的window + EditSalary()
        /// <summary>
        /// 加载修改工资记录的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditSalary(int Id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_salaryBLL.GetListBy(x => x.SalaryDisID == Id).FirstOrDefault().ToPOCO().ToViewModel();
            return PartialView("WriteSalary", model);
        }
        #endregion

        #region post加载修改工资过的记录--EditSalary
        /// <summary>
        ///post加载修改工资过的记录 SaveSalary(MODEL.qgzx_salary model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditSalary(MODEL.qgzx_salary model)
        {
            model.DistriTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_salaryBLL.Modify(model, "SalarySource", "GrantMethod", "DrawReason", "SalaryStandard", "WorkingTime", "SumOfMoney", "BeginTime", "EndTime", "DistriTime");
            if (res > 0)
            {
                return Redirect("/admin/Performance/CheckSalary");
            }
            return Redirect("/admin/Performance/CheckSalary");
        }
        #endregion

        #region 加载审核工资详细的window + AuditSalary()
        /// <summary>
        /// 加载审核工资详细的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AuditSalary(int Id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_salaryBLL.GetListBy(x => x.SalaryDisID == Id).FirstOrDefault().ToPOCO().ToViewModel();
            return PartialView(model);
        }
        #endregion

        #region post工资发放审核的信息--AuditSalary
        /// <summary>
        ///post工资发放审核的信息 AuditSalary(MODEL.qgzx_performance model)
        /// </summary>
        /// <returns></returns>
        public ActionResult AuditSalary(MODEL.qgzx_salary model)
        {
            model.AuditTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_salaryBLL.Modify(model, "SalarySource", "GrantMethod", "DrawReason", "SalaryStandard", "WorkingTime", "SumOfMoney", "BeginTime", "EndTime", "IsConvert", "AuditStatus", "AuditReason", "AuditTime", "IsGrantSuccess");
            if (res > 0)
            {
                return Redirect("/admin/Performance/CheckSalary");
            }
            return Redirect("/admin/Performance/CheckSalary");
        }
        #endregion

        #region 删除工资记录 + DelSalary()
        /// <summary>
        /// 删除工资记录 + DelPerfor()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelSalary()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_salaryBLL.DelBy(x => x.SalaryDisID == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion

        //------------业务搜索功能---------------------//
        #region 岗位申请的搜索功能--ActionResult ApplySearch()
        /// <summary>
        /// 搜索功能 需要进一步优化
        /// </summary>
        /// <returns></returns>
        public ActionResult ApplySearch()
        {
            string PositionName = Request.QueryString["PositionName"];
            string ApplyStudentName = Request.QueryString["ApplyStudentName"];
            string ApplyStudentNum = Request.QueryString["ApplyStudentNum"];
            string DistriReason = Request.QueryString["DistriReason"];
            string ReleaseTime = Request.QueryString["ReleaseTime"];
            string ApplyDate = Request.QueryString["ApplyDate"];

            int pageSize = int.Parse(Request.Form["rows"]);
            int pageIndex = int.Parse(Request.Form["page"]);

            var lists = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetPagedList(pageIndex, pageSize, x => x.AuditResult == 1 && x.DistriResult == 1 && x.IsOnGuard == 0, y => y.PositionId);
            if (PositionName != "")
            {
                lists = lists.Where(x => x.PositionName.ToString().Contains(PositionName)).ToList();
            }
            if (ApplyStudentName != "")
            {
                lists = lists.Where(x => x.ApplyStudentName.Contains(ApplyStudentName)).ToList();
            }
            if (ApplyStudentNum != "")
            {
                lists = lists.Where(x => x.ApplyStudentNum.Contains(ApplyStudentNum)).ToList();
            }
            if (DistriReason != "")
            {
                lists = lists.Where(x => x.DistriReason.ToString().Contains(DistriReason)).ToList();
            }
            if (ReleaseTime != "")
            {
                lists = lists.Where(x => x.ReleaseTime.ToString().Contains(ReleaseTime)).ToList();
            }
            if (ApplyDate != "")
            {
                lists = lists.Where(x => x.ApplyDate.ToString().Contains(ApplyDate)).ToList();
            }

            int rowCount = lists.Count();
            MODEL.EasyUIModel.DataGridModel dgModel = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = lists,
                footer = null
            };

            return Json(dgModel);
        }
        #endregion

        #region 绩效考核的搜索功能--ActionResult PerforSearch()
        /// <summary>
        /// 搜索功能 需要进一步优化
        /// </summary>
        /// <returns></returns>
        public ActionResult PerforSearch()
        {
            string PositionName = Request.QueryString["PositionName"];
            string WorkStudentNum = Request.QueryString["WorkStudentNum"];
            string WorkStudentName = Request.QueryString["WorkStudentName"];
            string WorkEvaluate = Request.QueryString["WorkEvaluate"];
            string AbsentTimes = Request.QueryString["AbsentTimes"];
            string PerforScore = Request.QueryString["PerforScore"];
            string AuditStatus = Request.QueryString["AuditStatus"];

            int pageSize = int.Parse(Request.Form["rows"]);
            int pageIndex = int.Parse(Request.Form["page"]);

            var lists = OperateContext.Current.BLLSession.Iqgzx_performanceBLL.GetPagedList(pageIndex, pageSize, x => x.RegisterTime != null, y => y.PositionId);
            if (PositionName != "")
            {
                lists = lists.Where(x => x.PositionName.ToString().Contains(PositionName)).ToList();
            }
            if (WorkStudentNum != "")
            {
                lists = lists.Where(x => x.WorkStudentNum.Contains(WorkStudentNum)).ToList();
            }
            if (WorkStudentName != "")
            {
                lists = lists.Where(x => x.WorkStudentName.Contains(WorkStudentName)).ToList();
            }
            if (WorkEvaluate != "")
            {
                lists = lists.Where(x => x.WorkEvaluate.ToString().Contains(WorkEvaluate)).ToList();
            }
            if (AbsentTimes != "")
            {
                lists = lists.Where(x => x.AbsentTimes.ToString().Contains(AbsentTimes)).ToList();
            }
            if (PerforScore != "")
            {
                lists = lists.Where(x => x.PerforScore.ToString().Contains(PerforScore)).ToList();
            }
            if (AuditStatus != "")
            {
                lists = lists.Where(x => x.AuditStatus.ToString().Contains(AuditStatus)).ToList();
            }

            int rowCount = lists.Count();
            MODEL.EasyUIModel.DataGridModel dgModel = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = lists,
                footer = null
            };

            return Json(dgModel);
        }
        #endregion

        #region 工资详细的搜索功能--ActionResult SalarySearch()
        /// <summary>
        /// 搜索功能 需要进一步优化
        /// </summary>
        /// <returns></returns>
        public ActionResult SalarySearch()
        {
            string PositionName = Request.QueryString["PositionName"];
            string WorkStudentName = Request.QueryString["WorkStudentName"];
            string WorkStudentNum = Request.QueryString["WorkStudentNum"];
            string SalarySource = Request.QueryString["SalarySource"];
            string GrantMethod = Request.QueryString["GrantMethod"];
            string DrawReason = Request.QueryString["DrawReason"];
            string SalaryStandard = Request.QueryString["SalaryStandard"];
            string BeginTime = Request.QueryString["BeginTime"];
            string EndTime = Request.QueryString["EndTime"];

            int pageSize = int.Parse(Request.Form["rows"]);
            int pageIndex = int.Parse(Request.Form["page"]);

            var lists = OperateContext.Current.BLLSession.Iqgzx_salaryBLL.GetPagedList(pageIndex, pageSize, x => x.SalaryDisID != null, y => y.PositionId);
            if (PositionName != "")
            {
                lists = lists.Where(x => x.PositionName.ToString().Contains(PositionName)).ToList();
            }
            if (WorkStudentName != "")
            {
                lists = lists.Where(x => x.WorkStudentName.Contains(WorkStudentName)).ToList();
            }
            if (WorkStudentNum != "")
            {
                lists = lists.Where(x => x.WorkStudentNum.Contains(WorkStudentNum)).ToList();
            }
            if (SalarySource != "")
            {
                lists = lists.Where(x => x.SalarySource.ToString().Contains(SalarySource)).ToList();
            }
            if (GrantMethod != "")
            {
                lists = lists.Where(x => x.GrantMethod.ToString().Contains(GrantMethod)).ToList();
            }
            if (DrawReason != "")
            {
                lists = lists.Where(x => x.DrawReason.ToString().Contains(DrawReason)).ToList();
            }
            if (SalaryStandard != "")
            {
                lists = lists.Where(x => x.SalaryStandard.Contains(SalaryStandard)).ToList();
            }
            if (BeginTime != "")
            {
                lists = lists.Where(x => x.SalarySource.ToString().Contains(BeginTime)).ToList();
            }
            if (EndTime != "")
            {
                lists = lists.Where(x => x.EndTime.ToString().Contains(EndTime)).ToList();
            }
            int rowCount = lists.Count();
            MODEL.EasyUIModel.DataGridModel dgModel = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = lists,
                footer = null
            };

            return Json(dgModel);
        }
        #endregion
    }
}
